//
//  MyCocos2DClass.h
//  BeautyGuessFree
//
//  Created by Charles How on 12-4-15.
//  Copyright 2012年 hust. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GameCenterLayer.h"

#import "storage.h"
#import "GameMusicSetting.h"


@implementation GameCenterLayer
-(id) init
{
	if( (self=[super init]) ) {
		
		
        
		//init array
		menuItemArray=[[NSMutableArray alloc] init];
		
		CGSize s = [[CCDirector sharedDirector] winSize];
		
		//bg
		CCSprite * bg=[CCSprite spriteWithFile:@"game-center-list-bg.png"];
		bg.position=ccp(s.width/2,s.height/2);
		
        float scaleX=1.0,scaleY=1.0;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
                scaleX=s.width/(float)bg.contentSize.width;
                scaleY=s.height/(float)bg.contentSize.height;
        }
        else{
                scaleX=1.0;
                scaleY=1.0;
        }
        
        bg.scaleX=scaleX;
        bg.scaleY=scaleY;
        [self addChild:bg z:0 tag:100];
		
        
  		//add board BG
        [self addBoardBG];
		[self addBoardContent];
        
        //back button
        [self addBackButton];
		
		
        
        //ad-bg
        CCSprite * ad_bg=[CCSprite spriteWithFile:@"game-center-ad-bg.png"];
        ad_bg.position=ccp(s.width/2,ad_bg.contentSize.height/2);
        [self addChild:ad_bg z:0 tag:1001];
        ad_bg.visible=false;
	}
	
	return self;
}



-(void)addBoardBG
{
    CGSize s=[[CCDirector sharedDirector] winSize];
    
    NSString * path=[storage dataFilePathInResource:@"game-center-list-board.png"];
    UIImage* image=[UIImage imageWithContentsOfFile:path];
    int ori_image_w=image.size.width;
    int ori_image_h=image.size.height;
    float scale=((float)s.width)/MAX_GC_LAYER_BOARD_PER_LINE/image.size.width;  //每行2个面板
    UIImage* image2=[self resizeImage:image andScaleX:scale andScaleY:scale];
    

    
    for (int i=0; i<MAX_GC_LAYER_BOARD_PER_LINE; i++) {
        CCSprite* board=[CCSprite spriteWithCGImage:image2.CGImage key:@"BOARD_PIC_GC"];
        CCSprite* board2=[CCSprite spriteWithCGImage:image2.CGImage key:@"BOARD_PIC_GC_2"];
        board2.color=ccc3(255, 255, 0);
        
        CCMenuItemSprite *item1 = [CCMenuItemSprite itemFromNormalSprite:board
                                                          selectedSprite:board2
                                                                  target:self selector:@selector(menuCallback_GC:)];
        //item1.color=ccc3(255, 0, 0);
        
        /*
         CCMenuItemFont *item2 = [CCMenuItemFont itemFromString: item.name 
         target:self selector:@selector(menuCallback_Link:)];
         item2.color=ccc3(0, 255, 0);
         */
        
        [menuItemArray addObject:item1];  //index 0
    }
    
    
    
    assert([menuItemArray count] <= MAX_GC_LAYER_BOARD_PER_LINE);
    
    menu = [CCMenu menuWithItems:
            [menuItemArray objectAtIndex:0], 
            [menuItemArray objectAtIndex:1],
            nil]; // 4 items.
    
    [menu alignItemsHorizontally];
    
    
    
    
    menu.position=ccp(s.width/2
                      , s.height/2);
    int width=menu.contentSize.width;
    int height=menu.contentSize.height;
    
    
    [self addChild:menu z:5 tag:GC_MENU_ITEM_ALL_BOARD];
    
    ///test code
    /*
    CCSprite * board2=[CCSprite spriteWithFile:@"button_other_003.png"];
    board2.position=ccp(s.width/2
                       , s.height/2);
    [self addChild:board2 z:100];
     */
}


-(void)addBackButton
{
    // Image Item  ,back button
    CCSprite * tmpSprite=[CCSprite spriteWithFile:@"game-center-quit0.png"];
    CGSize winSize=[[CCDirector sharedDirector] winSize];
    back = [CCMenuItemImage 
            itemFromNormalImage:@"game-center-quit0.png" 
            selectedImage:@"game-center-quit1.png" 
            target:self 
            selector:@selector(backCallback:)];
    CCMenu *menuBack = [CCMenu menuWithItems:
                        back,nil];
    [menuBack alignItemsHorizontally];
    menuBack.position=ccp(winSize.width-tmpSprite.contentSize.width/2,tmpSprite.contentSize.height/2); ///右下角
    [self addChild: menuBack z:5 tag:GC_MENU_ITEM_BACK];
}


///在面板上画图标和写字，排行榜与成就系统
-(void)addBoardContent
{
    int index=0;
    for (CCNode * child in [menu children]) {
        if(index == 0){
            //image
            CCSprite * LB=[CCSprite spriteWithFile:@"game-center-icon-lb.png"];
            LB.position=ccp(menu.position.x+child.position.x
                            , menu.position.y+child.position.y);
            LB.scale=(float)child.contentSize.width/2/LB.contentSize.width;
            [self addChild:LB z:10];
            
            //label
            NSString * fontName=[NSString stringWithString:@"Marker Felt"];  //@"Marker Felt"
            CCLabelTTF * label=[CCLabelTTF labelWithString:
                                NSLocalizedString(@"Leaderboard", nil) 
                                                  fontName:fontName 
                                                  fontSize:LB.contentSize.width/16];
            label.position=ccp(LB.position.x, 
                               LB.position.y-LB.contentSize.height*LB.scale/2-label.contentSize.height/2);
            label.color=ccc3(0, 255, 0);
            [self addChild:label z:10 ];
        }
        else if(index == 1){
            CCSprite * AC=[CCSprite spriteWithFile:@"game-center-icon-ac.png"];
            AC.position=ccp(menu.position.x+child.position.x
                            , menu.position.y+child.position.y);
            AC.scale=(float)child.contentSize.width/2/AC.contentSize.width;
            [self addChild:AC z:10];
            
            
            
            
            //label
            NSString * fontName=[NSString stringWithString:@"Marker Felt"];  //@"Marker Felt"
            CCLabelTTF * label=[CCLabelTTF labelWithString:NSLocalizedString(@"Achievements", nil)
                                                  fontName:fontName 
                                                  fontSize:AC.contentSize.width/16];
            label.position=ccp(AC.position.x, 
                               AC.position.y-AC.contentSize.height*AC.scale/2-label.contentSize.height/2);
            label.color=ccc3(0, 255, 0);
            [self addChild:label z:10 ];
        }
            
        
        index++;
    }
}


-(void)menuCallback_GC:(id)sender
{
    [GameMusicSetting playButtonClickEffect];
    
    
    int i=0;
    for( CCNode *child in [menu children] ) {
        
        if (sender == child) {
            break;
        }
        
        i++;
    }
    
    
    if (i == 0) {
        [self menuCallback_LB:nil];
    }
    else if(i == 1){
        [self menuCallback_ACH:nil];
    }
}


-(void)menuCallback_LB:(id)sender
{
    [GameMusicSetting playButtonClickEffect];
    
    GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper showLeaderboard];
}



-(void)menuCallback_ACH:(id)sender
{
    [GameMusicSetting playButtonClickEffect];
    
    GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper showAchievements];
}


-(void) backCallback: (id) sender
{
    [GameMusicSetting playButtonClickEffect];
    
	//back to game
	[[CCDirector sharedDirector] popScene];
	
}


- (void) dealloc
{
	[super dealloc];
}



//////////////////////////////////////////////ad begin
#pragma Marker adMobBanner
- (void)onEnter
{
	[super onEnter];
	
	adController = [[AdViewController alloc] initWithMasterView:[[CCDirector sharedDirector] openGLView]];
	[adController setDelegate:self];
	
}

- (void)onExit
{
	//Completely remove the controller
	[adController setDelegate:nil];
	[adController release];
	adController = nil;
	
	[super onExit];
}

- (void)adController:(AdViewController*)controller didLoadiAd:(id)iadBanner;
{
	//Do something when the ad loads, like moving objects.
    
    
    CCSprite * ad_bg=[self getChildByTag:1001];
    ad_bg.visible=true;
}

- (void)adController:(AdViewController*)controller didFailedToRecieveiAd:(id)iadBanner;
{
	//Do something when the ad fails to load, like moving objects.;
    CCSprite * ad_bg=[self getChildByTag:1001];
    ad_bg.visible=false;
}

- (void)adController:(AdViewController*)controller didLoadAdMobAd:(ADBannerView*)adMobBanner;
{
	//Do something when the ad loads, like moving objects.
    CCSprite * ad_bg=[self getChildByTag:1001];
    ad_bg.visible=true;
}

- (void)adController:(AdViewController*)controller didFailedToRecieveAdMobAd:(ADBannerView*)adMobBanner;
{
	//Do something when the ad fails to load, like moving objects.
    CCSprite * ad_bg=[self getChildByTag:1001];
    ad_bg.visible=false;
}

//////////////////////////////ad end



/*
 对UIImage进行缩放的函数
 */
-(UIImage *)resizeImage:(UIImage *)image andScaleX:(float)scaleX andScaleY:(float)scaleY
{
    // original
    //UIImage *image = [UIImage imageNamed:@"icon.png"];
    
    CGRect newRect;
    newRect.size.height = image.size.height*scaleY;
    newRect.size.width = image.size.width*scaleX;
    newRect.origin = (CGPoint){0.0f, 0.0f};
    UIGraphicsBeginImageContext(newRect.size);
    [image drawInRect:newRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //使用 newImage 即可
    return newImage;
}

@end



